<template>
  <div class="addAndEdit base-addAndEdit">
    <h3>产品信息</h3>
    <el-form :model="form" label-width="100px" :disabled="type == 'view'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品编号">
            <el-input v-model="form.productNum" placeholder="请输入产品编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品名称">
            <el-input v-model="form.productName" placeholder="请输入产品名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出发城市">
            <el-input v-model="form.cityName" placeholder="请输入出发城市" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出发时间">
            <el-date-picker
              style="width: 100%"
              v-model="form.departureTime"
              type="datetime"
              placeholder="请选择出发时间"
              value-format="YYYY-MM-DD hh:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品价格">
            <el-input
              v-model="form.productPrice"
              placeholder="请输入产品价格"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户状态">
            <el-select v-model="form.status" placeholder="请选择用户状态">
              <el-option label="开启" value="1" />
              <el-option label="关闭" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="产品描述">
            <el-input
              v-model="form.productDesc"
              type="textarea"
              maxlength="30"
              show-word-limit
              placeholder="请输入产品描述"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row justify="center">
      <template v-if="type == 'add'">
        <el-button type="primary" @click="handleSaveOrBack(true)"
          >保存</el-button
        >
      </template>
      <el-button @click="handleSaveOrBack(false)">返回</el-button>
    </el-row>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import Mitt from "@/mitt/index.js";

const emits = defineEmits(["sendData"]);
//从viewAll传递到的数据，用来判断当前组件的状态是添加还是编辑
const { currentShow } = defineProps(["currentShow"]);
//defineProps不支持二次解构
const { type, row } = currentShow;

const form = ref({
  productNum: "",
  productName: "",
  cityName: "",
  departureTime: "",
  productPrice: "",
  productDesc: "",
});

const handleSaveOrBack = (type) => {
  //如果是保存，调用保存的接口，否则直接返回
  if (type) {
    console.log(form.value);
  }

  emits("sendData", {
    showComponents: "viewAll",
    type: "",
  });
};

//表单数据回显
const getList = () => {
  //通过传递的数据id请求接口
  console.log(row);
};

onMounted(() => {
  Mitt.emit("sendcrumbsInfo", { first: "产品管理", second: "产品表单" });

  if (type != "add") {
    //如果状态不处在添加状态时，需要请求接口回显数据
    getList();
  }
});
</script>

<style lang="less" scoped></style>
